package com.example.easycharge.schedule;

import com.example.easycharge.entity.ChargePile;
import com.example.easycharge.entity.Report;
import com.example.easycharge.repository.ChargePileRepository;
import com.example.easycharge.repository.ReportRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Component
public class PersistenceSchedule {

    @Autowired
    private ChargePileRepository chargePileRepository;
    @Autowired
    private ReportRepository reportRepository;

    // 定时任务，每一分钟秒执行一次，程序启动十秒钟后再开始定时任务
//    @Scheduled(fixedDelay = 60000, initialDelay = 10000)
//    public void persistence() {
//        // TODO: 2022-03-29 将队列信息持久化到数据库中
//
//    }

    @Scheduled(cron = "0 55 23 * * ?")
    public void reportGenerate() {
        // 每天晚上23点55分生成当日报表
        List<ChargePile> allPile = chargePileRepository.findAll();

        for (ChargePile chargePile : allPile) {
            reportRepository.save(new Report(chargePile));
        }
    }
}
